package com.sanchunhui.psc.ui.activity

import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.util.Log
import androidx.annotation.RequiresApi
import com.eightbitlab.rxbus.Bus
import com.j256.ormlite.stmt.query.In
import com.sanchunhui.psc.R
import com.sanchunhui.psc.event.BlueClickConnectEvent
import com.sanchunhui.psc.event.DeviceInfoReadEvent
import com.sanchunhui.psc.utils.bleuitls.callback.OnReceiverCallback
import kotlinx.android.synthetic.main.activity_device_info.*
import kotlin.experimental.and

/**
 * @Author zyx
 * @Date 2021/6/25-13:04
 * @Dscribe
 */
class DeviceInfoActivity : BaseActivity() {

    private val TAG = "DeviceInfoActivity"

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    val deviceListActivity : DeviceListActivity = DeviceListActivity()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_device_set_info)
        initObserve()
    }

    /*
       注册监听
    */
    private fun  initObserve() {
        Bus.observe<DeviceInfoReadEvent>().subscribe {
            run {
//                itemStateTv = this.findViewById(R.id.blue_item_status_tv)
//                Log.e("info", it.devArray.toString())
                if (it.devArray.size > 0) {
                    // 解析数据
                    // 序列号
                    var  numberStr : Int = 0
                    numberStr = convertFourInt(it.devArray,1,2,3,4)
                    //  电量
                    var batStr : Int = 0
                    batStr = convertTwoInt(it.devArray, 6,5)
                    // MS5611温度
                    var temp_aStr : Int = 0
                    temp_aStr = convertTwoInt(it.devArray, 8,7)

                    //MS5611海拔高度
                    var alti_aStr = 0
                    alti_aStr = convertTwoInt(it.devArray, 10,9)

                    //BMP280温度
                    var temp_bStr = 0
                    temp_bStr = convertTwoInt(it.devArray,12,11)

                    //BMP280海拔高度
                    var alti_bStr = 0
                    alti_bStr = convertTwoInt(it.devArray, 14,13)

                    // BMP280湿度
                    var humi_bStr = 0
                    humi_bStr = convertTwoInt(it.devArray, 16,15)

                    //压力计算相对高度
                    var altipStr = 0
                    altipStr = convertTwoInt(it.devArray, 18,17)

                    //高度变化峰峰值
                    var altip_deltaStr = 0
                    altip_deltaStr = convertTwoInt(it.devArray,20,19)

                    //IMU-ax
                    var ax : Int = 0
                    ax = convertTwoInt(it.devArray, 22,21)

                    //IMU-ay
                    var ay : Int = 0
                    ay = convertTwoInt(it.devArray, 24,23)

                    //IMU-az
                    var az : Int = 0
                    az = convertTwoInt(it.devArray ,26,25)

                    //IMU-计算值
                    var amqStr : Int = 0
                    amqStr = convertTwoInt(it.devArray,28,27)

                    // 移动状态
                    var moveStr : Int =  it.devArray[29].toInt() and 0xFF
                    //压力传感器
                    var pressStr : Int =  it.devArray[30].toInt() and 0xFF
                    // 红外信号状态
                    var birStr : Int =  it.devArray[31].toInt() and 0xFF
                    // 安全状态 1、系好安全带 0、没系安全带
                    var lockStr : Int =  it.devArray[32].toInt() and 0xFF
                    // 报警状态
                    var warnStr : Int =  it.devArray[33].toInt() and 0xFF


                    runOnUiThread {
                        mNumberTv.text = numberStr.toString() // 序列号
                        mBatTv.text = batStr.toString()+"%" // 电量
                        mTemp_aTv.text = (temp_aStr.toDouble()/100).toString() // MS5611温度
                        mAlti_aTv.text = (alti_aStr.toDouble()/10).toString() //MS5611海拔高度
                        mTemp_bTv.text = (temp_bStr.toDouble()/100).toString() //BMP280温度
                        mAlti_bTv.text = (alti_bStr.toDouble()/10).toString() //BMP280海拔高度
                        mHumi_bTv.text = (humi_bStr.toDouble()/100).toString() // BMP280湿度
                        mAltipTv.text = (altipStr.toDouble()/10).toString() //压力计算相对高度
                        mAltip_deltaTv.text = (altip_deltaStr.toDouble()/10).toString() //高度变化峰峰值
                        mAxTv.text = ax.toInt().toString() //IMU-ax
                        mAyTv.text = ay.toInt().toString() //IMU-ay
                        mAzTv.text = az.toInt().toString() //IMU-az
                        mAmqTv.text = amqStr.toInt().toString() // IMU-计算值
                        mMoveTv.text = moveStr.toString() // 移动状态
                        mPressTv.text= pressStr.toString() // 压力传感器
                        mBirTv.text = birStr.toString() // 红外信号状态
                        mLockTv.text = lockStr.toString() // 安全状态
                        mWarnTv.text = warnStr.toString() // 报警状态
                    }
                }

            }
        }
    }

    // 2字节
    fun convertTwoInt(byteArray: ByteArray, indexH : Int, indexL: Int): Int {
        if (byteArray[indexH] < 0 || byteArray[indexL] < 0) {
            return (byteArray[indexH].toInt() shl 8) or (byteArray[indexL].toInt() and 0xFF)
        } else {
            return (byteArray[indexH].toInt() and 0xFF) shl 8 or (byteArray[indexL].toInt() and 0xFF)
        }
    }

    // 4字节
    fun convertFourInt(byteArray: ByteArray, index1 : Int, index2: Int,index3 : Int, index4: Int): Int {
        if (byteArray[index1] < 0 || byteArray[index1] < 0|| byteArray[index3] < 0 || byteArray[index4] < 0) {
            return (byteArray[index4].toInt() shl 24) or (byteArray[index3].toInt() and 0xFF) or (byteArray[index2].toInt() shl 8) or (byteArray[index1].toInt() and 0xFF)
        } else {
            return (byteArray[index4].toInt() and 0xFF)  shl 24 or (byteArray[index3].toInt() and 0xFF) shl 16 or (byteArray[index2].toInt() and 0xFF) shl 8 or (byteArray[index1].toInt() and 0xFF)
        }
    }

}